<!-- 修改组织架构 -->
<template>
  <dt-modal-edit ref='info' :title='title' :label-width='100' okText='确定' :width='60' @ok='onOk' :okDisabled='OKFlag'>
    <div class='change_organiza'>
      <div slot="bottom">
        <div class="target_total">
          <div class="target_mouth">
            <span>{{infoData.firstLine.label}}</span><span>{{infoData.firstLine.value}}</span>
          </div>
          <div class="target_mouth second_line">
            <span>{{infoData.secondLine.label}}</span>
            <div>
              {{infoData.secondLine.value}}
            </div>
          </div>
        </div>
      </div>
      <div class="tree_warp">
        <div class="tree_top_txt">
          {{infoData.treeTitle}}
        </div>
        <Tree :data="treeList" :render="renderContent" @on-select-change="treeChange" class="tree_render"></Tree>
      </div>
    </div>
  </dt-modal-edit>
</template>

<script>
  import organApi from '@/biz/option/businessOrgan'

  export default {
    components: {},
    data() {
      return {
        title: '修改案场所属',
        infoData: {
          firstLine: {},
          secondLine: {},
          treeTitle: '',
        },
        treeList: [],
        curTreeId: '',
        OKFlag: false
      }
    },
    methods: {
      renderContent() {
        return (h, {
          data
        }) => {
          return h('span', {
            style: {
              display:'inline-block',
              backgroundColor: data.disabled ? '#f7f7f7' : '',
              color: data.disabled ? '#c5c8ce' : '',
              padding: '0 4px',
              lineHeight: '1.5'
            },
          }, data.title)
        }
      },
      show(data) {
        console.log(data)
        this.infoData = {
          ...data
        }
        this.$refs.info.show()
        this.getDepartmentList()
      },
      hide() {
        this.$refs.info.hide()
      },
      onOk() {
        if (!this.curTreeId) {
          this.$Message.warning('请先选择一个组织！')
          return false
        }
        this.$emit('success', this.curTreeId)
        this.hide()
      },
      //循环遍历部门
      forEachDepartment(mess) {
        mess.map((item) => {
          item.title = item.orgName
          item.render = this.renderContent()
          item.disabled = this.infoData.secondLine.checkId == item.orgId ? true : false
          item.children && this.forEachDepartment(item.children)
        })
      },
      //获取部门列表
      getDepartmentList() {
        organApi.getWebOrg().then((res) => {
          if (res) {
            res.forEach((ms) => {
              ms.title = ms.orgName
              ms.expand = true
              ms.render = this.renderContent()
              ms.disabled = this.infoData.secondLine.checkId == ms.orgId ? true : false
              ms.children && this.forEachDepartment(ms.children)
            })
            this.treeList = res
            console.log('部门列表treeList', this.treeList)
          }
        })
      },
      treeChange(event) {
        console.log(event, '点击了树结构')
        if (event[0]) {
          if (event[0].level == 1) {
            this.OKFlag = true
          } else {
            this.OKFlag = false
            this.curTreeId = event[0] ? event[0].orgId : ''
          }
        } else {
          this.OKFlag = true
        }
      },
    },
  }
</script>
<style lang='scss' scoped>
  .change_organiza {
    .target_total {
      background-color: rgb(253 251 232);
      padding: 15px;
      margin-bottom: 15px;
      font-size: 16px;
      line-height: 30px;
    }

    .tree_warp {
      .tree_top_txt {
        font-size: 16px;
      }

      .tree_render {
        height: 500px;
        overflow: auto;
      }
    }

    .second_line {
      display: flex;
    }
  }
</style>